package com.weixing.mall.provider.controller.front;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.provider.api.model.enums.ActTypeEnum;
import com.weixing.mall.provider.model.domain.Activity;
import com.weixing.mall.provider.model.enums.act.IsPublish;
import com.weixing.mall.provider.model.query.ActivityQuery;
import com.weixing.mall.provider.model.query.act.ActGoodsQuery;
import com.weixing.mall.provider.model.vo.act.ActivityGoodsVo;
import com.weixing.mall.provider.service.IActivityGoodsService;
import com.weixing.mall.provider.service.IActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ceshi
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2020/1/911:50
 */
@RestController
@RequestMapping("front/activity")
@Api(value = "前端活动API", tags = "前端活动")
public class FrontActivityController extends BaseController {
    @Autowired
    private IActivityService activityService;
    @Autowired
    private IActivityGoodsService activityGoodsService;

    @ApiOperation(value = "展示活动类型和活动名称")
    @GetMapping("")
    public Result list(@MemInfo MemAuth auth, Long partitionId, Integer client) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("type", ActTypeEnum.values());
        ActivityQuery query = new ActivityQuery();
        query.setUid(auth.getMid());
        query.setAgentId(auth.getAid());
        query.setPartitionId(partitionId);
        query.setClient(client);
        query.setNowDate(DateUtil.formatDateTime(new Date()));
        if (auth.getAbid() == null) {//若只是会员将过滤掉代理商活动展示
            query.setActType(ActTypeEnum.ACT_SECKILL);
        }
        // 为了不想改动代理，这里将分页设置大一点，间接实现前台不分页的效果
        Page page = new Page(1,100);
        IPage pageInfo = activityService.findList(page, query);
        result.put("data", pageInfo);
        return ResultUtil.success(result);
    }

    /**
     * 二期更改了需求，不在限制显示的内容
     * @param partitionId
     * @param client
     * @return
     */
    @ApiOperation(value = "展示活动类型和活动名称")
    @GetMapping("/frontList")
    @NoNeedAccess
    public Result frontList( Long partitionId, Integer client) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("type", ActTypeEnum.values());
        ActivityQuery query = new ActivityQuery();
       /* query.setUid(auth.getMid());
        query.setAgentId(auth.getAid());*/
        query.setPartitionId(partitionId);
        query.setIsPublish(IsPublish.IS_PUBLISH);
        query.setClient(client);
      /*  if (auth.getAbid() == null) {//若只是会员将过滤掉代理商活动展示
            query.setActType(ActTypeEnum.ACT_SECKILL);
        }
        if(null!=auth.getAbid()&&auth.getAbid().longValue()==0){
            query.setActType(ActTypeEnum.ACT_SECKILL);
        }*/
        // 为了不想改动代理，这里将分页设置大一点，间接实现前台不分页的效果
        Page page = new Page(1,100);
        IPage pageInfo = activityService.findFrontList(page, query);
        result.put("data", pageInfo);
        return ResultUtil.success(result);
    }
    @ApiOperation(value = "活动商品")
    @GetMapping("/goodsList")
    public Result goodsList(ActGoodsQuery actGoodsQuery) {
        IPage<ActivityGoodsVo> list = activityGoodsService.findList(actGoodsQuery, initMpPage());
        return ResultUtil.success(list);

    }

    @ApiOperation(value = "返回活动开始时间")
    @GetMapping("/gettimeGap/{id}")
    @NoNeedAccess
    public Result actEntity(@PathVariable Long id) {
        Activity entity = activityService.getById(id);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("startFlag", entity.getStartTime().getTime());//多久可以開始，如果小于0代表沒有開始
        result.put("endFlag", entity.getEndTime().getTime());//是否已經結束，如果大于0代表已經結束
        result.put("title", entity.getActName());//是否已經結束，如果大于0代表已經結束
        result.put("actType",entity.getActType().equals(ActTypeEnum.ACT_AGENT.getValue())?ActTypeEnum.ACT_AGENT:ActTypeEnum.ACT_SECKILL);
        return ResultUtil.success(result);
    }

    @ApiOperation(value = "根据活动查询一年之内有效的活动列表")
    @GetMapping("/byActType/{type}")
    public Result byActId(@PathVariable Long type) {
        Date dateTime = DateUtil.offset(new Date(), DateField.YEAR, -1);
        List<Activity> activityList = activityService.list(new QueryWrapper<Activity>().gt("start_time",dateTime).eq("is_publish",WhetherEnum.YES.getValue()).eq("act_type",type).orderBy(true,false,"id"));
        return ResultUtil.success(activityList);
    }
    @ApiOperation(value = "根据有效地活动列表")
    @GetMapping("/byNewActType/{type}")
    @NoNeedAccess
    public Result byNewActType(@PathVariable Long type) {
        Date dateTime = DateUtil.offset(new Date(), DateField.YEAR, -1);
        List<Activity> activityList = activityService.list(new QueryWrapper<Activity>().gt("end_time",new DateTime()).eq("is_publish",WhetherEnum.YES.getValue()).eq("act_type",type).orderBy(true,false,"id"));
        return ResultUtil.success(activityList);
    }

//    @ApiOperation(value = "返回活动标题")
//    @GetMapping("/actTitle/{id}")
//    public Result actTitle(@PathVariable Long id) {
//        Activity entity = activityService.getById(id);
//        Map<String, Object> result = new HashMap<String, Object>();
//        result.put("startFlag", entity.getStartTime().getTime());//多久可以開始，如果小于0代表沒有開始
//        result.put("endFlag", entity.getEndTime().getTime());//是否已經結束，如果大于0代表已經結束
//        return ResultUtil.success(result);
//    }


}
